package com.beeasy.hzbpm.bean;

import cn.hutool.http.HttpUtil;
import com.github.llyb120.nami.core.Async;
import com.github.llyb120.nami.core.Config;
import com.github.llyb120.json.Obj;
import org.beetl.sql.core.SQLReady;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.github.llyb120.json.Json.*;


public class MessageSend {

//    private static APIClient handler = new APIClient();

    /**
     * 根据手机号码发送短信
     */
    public static void send(String phone, String message) {
        send(Collections.singletonList(phone), message);
    }

    public static void send(List<String> phones, String message) {
        Async.execute(() -> {
            //过滤掉不是手机号的
            List<String> phone = phones.stream()
                .filter(MessageSend::isMobile)
                .distinct()
                .collect(Collectors.toList());
            if (phone.isEmpty()) {
                return;
            }

//        ConnectionSource source = ConnectionSourceHelper.getSimple("com.ibm.db2.jcc.DB2Driver", "jdbc:db2://47.94.97.138:50000/test", "", "db2inst1", "db2inst1");
//        DB2SqlStyle style = new DB2SqlStyle();
//        SQLLoader loader = new ClasspathLoader("/sql");
//// 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的
//        UnderlinedNameConversion nc = new UnderlinedNameConversion();
//// 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
//        SQLManager sqlManager = new SQLManager(style, loader, source, nc, new Interceptor[]{new DebugInterceptor()});

            //无论如何都要发送一条消息
            String url = Config.config.var.ss("push-message-url");
            if(url.isEmpty()){
                return;
            }
            /*Obj params = o(
                    "phones", phone,
                    "content", message,
                    "timestamp", System.currentTimeMillis()
            );
            String rep = HttpUtil.get(url, o(
                    "param", params
            ));*/
            HttpUtil.createPost(url)
                .header("Content-Type", "application/json; charset=utf-8")
                .body(o(
                    "phones", phone,
                    "content", message,
                    "timestamp", new Date()
                ).toString())
                .timeout(30000)
                .executeAsync();
            if(true){
                return;
            }

            //检查短信接口是否开启
            //@fixme
//            List<Obj> vars = a();
//                //sqlManager.execute(new SQLReady("select count(1) from t_system_variable where var_name = 'msg_api_open' and var_value = '1'"), Obj.class);
//            if (vars.size() == 0 || vars.get(0).i("1") == 0) {
//                System.out.println("短信网关配置没有打开");
//                return;
//            }
//
//            int connectRe = handler.init("118.2.82.252", "hzrcbJava", "hzrcb@pwd2896880", "javaApiMsg", "mas");
//            if (connectRe != APIClient.IMAPI_SUCC) {
//                System.out.println("无法连接短信接口");
//                return;
//            }
//
//            Long srcId = 10086L;
//            // 发送
//            handler.sendSM(phone.toArray(new String[0]), message, srcId);

//        ShortMsg entity = new ShortMsg();
//        entity.setPhone(phone);
//        entity.setAddTime(new Date());
//        entity.setMessage(message);
//        entity.setState(0);
//        sqlManager.insert(entity);

            System.out.println("发送完毕");
        });
    }

    /**
     * 手机号验证
     */
    public static boolean isMobile(String mobile) {
        if (mobile == null) {
            return false;
        }
//        String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        String REGEX_MOBILE = "^[1][3,4,5,7,8][0-9]{9}$";
        return Pattern.matches(REGEX_MOBILE, mobile);
    }

}
